package top.xia17.plugins.fast.user.dao.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import top.xia17.plugins.fast.common.database.jpa.extension.BaseUserTimeEntity;
import top.xia17.plugins.fast.common.database.jpa.extension.JpaEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.Set;

/**
 * 用户实体
 * @author xia17
 * @date 2022/1/17
 */
@Entity
@Table(name = "sys_user")
@Accessors(chain = true)
@Getter
@Setter
@NoArgsConstructor
public class User extends BaseUserTimeEntity implements JpaEntity<Long>{

    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 用户名
     */
    @NotBlank
    private String username;

    /**
     * 密码
     */
    @JsonIgnore
    private String password;

    /**
     * 昵称
     */
    @NotBlank
    private String nickname;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 手机号
     */
    private String phone;

    private String email;

    /**
     * 是否启用
     */
    @NotNull
    private Boolean enabled;

    /**
     * 是否冻结 ,用于一些异常操作,比如密码连续错误
     */
    @NotNull
    private Boolean isFreeze;

    /**
     * 附加信息,方便用户拓展
     */
    private String extra;

    /**
     * 所属部门
     */
    @OneToOne
    private Dept dept;

    /**
     * 拥有的角色
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_users_roles",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
    private Set<Role> roles;

    /**
     * 权限信息
     */
    @Transient
    private Set<String> permissions;
}
